﻿using AutoMapper;
using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Infrastructure.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace B.MES.Customer.Application.Application
{
    public class RoleApplication : BaseApplication<Role>, IRoleApplication
    {
        /// <summary>
        /// RBAC仓储服务注入
        /// </summary>
        private readonly IRoleRepository _roleRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="roleRepository"> RBAC仓储服务</param>
        public RoleApplication(IRoleRepository roleRepository) : base(roleRepository)
        {
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<RoleDTO> AddRole(RoleDTO role)
        {
            try
            {
                return _roleRepository.AddRole(role);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="rolePermissionDTO">角色权限</param>
        /// <returns>角色权限的数据</returns>
        public APIResult<RolePermissionDTO> AddRolePermission(RolePermissionDTO rolePermissionDTO)
        {
           return _roleRepository.AddRolePermission(rolePermissionDTO);
        }

        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public APIResult<List<Role>> BatchDeleteRoles(List<Role> list)
        {
            return _roleRepository.BatchDeleteRoles(list);  
        }

        /// <summary>
        /// 单个删除角色（真删除）
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public APIResult<Role> DeleteRole(int roleId)
        {
            try
            {
                return _roleRepository.DeleteRole(roleId);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="role">角色信息</param>
        /// <returns>受影响的行数</returns>
        public APIResult<Role> UpdateRole(Role role)
        {
            return _roleRepository.UpdateRole(role);
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        /// <returns></returns>
        public PageApiResut<Role> GetRole()
        {
            try
            {
                return _roleRepository.GetRole();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 根据角色ID 获取对应的权限ID
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public PageApiResut<string> GetRoleIdByPermissionIds(string roleId)
        {
            try
            {
                return _roleRepository.GetRoleIdByPermissionIds(roleId);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
